package com.ml.carManager.utils.map;

import android.annotation.SuppressLint;

import com.amap.api.location.AMapLocation;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.RegeocodeResult;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@SuppressLint("SimpleDateFormat")
public class AMapUtil {

    public static boolean IsEmptyOrNullString(String s) {
        return (s == null) || (s.trim().length() == 0);
    }

    /**
     * 把LatLng对象转化为LatLonPoint对象
     */
    public static LatLonPoint convertToLatLonPoint(LatLng latlon) {
        return new LatLonPoint(latlon.latitude, latlon.longitude);
    }

    /**
     * 把LatLonPoint对象转化为LatLon对象
     */
    public static LatLng convertToLatLng(LatLonPoint latLonPoint) {
        return new LatLng(latLonPoint.getLatitude(), latLonPoint.getLongitude());
    }

    /**
     * 把集合体的LatLonPoint转化为集合体的LatLng
     */
    public static ArrayList<LatLng> convertArrList(List<LatLonPoint> shapes) {
        ArrayList<LatLng> lineShapes = new ArrayList<LatLng>();
        for (LatLonPoint point : shapes) {
            LatLng latLngTemp = AMapUtil.convertToLatLng(point);
            lineShapes.add(latLngTemp);
        }
        return lineShapes;
    }

    /**
     * long类型时间格式化
     */
    public static String convertToTime(long time) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(time);
        return df.format(date);
    }

    /**
     * 把高德返回的长地址转换为短地址</br> 比如：四川省成都市武侯区天府软件园D6转化为：</br> 武侯区天府软件园D6
     *
     * @param sourceAddress
     * @return
     */
    public static String getShortAddress(String sourceAddress,
                                         AMapLocation aMapLocation) {
        String shortAddress = "";
        if (sourceAddress == null || aMapLocation == null)
            return shortAddress;
        sourceAddress = sourceAddress.trim();
        if (sourceAddress.equals(""))
            return shortAddress;
        try {
            sourceAddress = sourceAddress.replace(aMapLocation.getProvince(),
                    "");
            sourceAddress = sourceAddress.replace(aMapLocation.getCity(), "");
            sourceAddress = sourceAddress.replace(aMapLocation.getDistrict(),
                    "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        shortAddress = sourceAddress;
        return shortAddress;
    }

    /**
     * 获取段地址
     *
     * @param sourceAddress
     * @param result
     * @return
     */
    public static String getShortAddress(String sourceAddress,
                                         RegeocodeResult result) {
        String shortAddress = "";
        if (sourceAddress == null || result == null)
            return shortAddress;
        sourceAddress = sourceAddress.trim();
        if (sourceAddress.equals(""))
            return shortAddress;
        try {
            sourceAddress = sourceAddress.replace(result.getRegeocodeAddress()
                    .getProvince(), "");
            sourceAddress = sourceAddress.replace(result.getRegeocodeAddress()
                    .getCity(), "");
            sourceAddress = sourceAddress.replace(result.getRegeocodeAddress()
                    .getDistrict(), "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        shortAddress = sourceAddress;
        return shortAddress;
    }

    /**
     * 计算两个坐标之间的距离
     *
     * @param lat1
     * @param lng1
     * @param lat2
     * @param lng2
     * @return
     */
    public static Double GetDistance(Double lat1, Double lng1, Double lat2,
                                     Double lng2) {
        Double EARTH_RADIUS = 6378.137;
        Double radLat1 = lat1 * Math.PI / 180.0;
        Double radLat2 = lat2 * Math.PI / 180.0;
        Double a = radLat1 - radLat2;
        Double b = (lng1 * Math.PI / 180.0) - (lng2 * Math.PI / 180.0);
        Double s = 2 * Math.sin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
                + Math.cos(radLat1) * Math.cos(radLat2)
                * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        BigDecimal _BigDecimal = new BigDecimal(s);
        double result = _BigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP)
                .doubleValue();
        return result;
    }
    /**
     * 移动到坐标位置
     *
     * @param aMap
     * @param latitude
     * @param longitude
     * @param scaleSize
     */
    public static void moveToPosition(AMap aMap, Double latitude,
                                      Double longitude, float scaleSize) {
        animateCamera(aMap, latitude, longitude, scaleSize, 500);
    }

    /**
     * 移动到坐标位置
     *
     * @param aMap
     * @param latitude
     * @param longitude
     * @param scaleSize
     */
    public static void moveToPosition(AMap aMap, Double latitude,
                                      Double longitude, float scaleSize, int duration) {
        animateCamera(aMap, latitude, longitude, scaleSize, duration);
    }
    /**
     * 动画的方式到定位点
     *
     * @param aMap
     * @param atitude
     * @param longitude
     * @param scaleSize
     */
    public static void animateCamera(AMap aMap, Double atitude,
                                     Double longitude, float scaleSize, int duration) {
        LatLng latLng = new LatLng(atitude, longitude);
        CameraPosition cameraPosition = new CameraPosition(latLng, scaleSize,
                0, 0);
        CameraUpdate cameraUpdate = CameraUpdateFactory
                .newCameraPosition(cameraPosition);
        aMap.animateCamera(cameraUpdate, duration, null);
    }
}
